package org.tgcloud.group.web.controller.manager.export;

import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.tgcloud.group.service.manager.export.MediumDateCountExportManageService;
import org.tgcloud.group.web.controller.manager.ManageAPPBaseController;

import javax.servlet.http.HttpServletResponse;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author: zhy
 * @Created: 2024-06-11-下午4:31
 * @Description:
 */
@Api(tags = "导出相关接口")
@RestController
@RequestMapping("/manager/pc/user")
public class MediumDateCountExportManageController extends ManageAPPBaseController {

    @Autowired
    private MediumDateCountExportManageService mediumDateCountExportManageService;

    @ApiOperation(value = "媒体日历导出统计")
    @ApiImplicitParams({
    })
    @ApiResponses({
            @ApiResponse(code = 2000, message = "出现异常，及时通知后台")
    })
    @ResponseBody
    @PostMapping(value = "/doExportMediumDateCount")
    public void doExportCountMediumDateFile(HttpServletResponse response,
                                       @RequestParam Long startTime,
                                       @RequestParam Long endTime) throws Exception{
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/octet-stream;charset=UTF-8");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
        String excelName = dateFormat.format(new Date(startTime)) + "-" + dateFormat.format(new Date(endTime)) + "媒体日历导出统计";
        response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(excelName, "UTF-8") + ".xls");
        response.addHeader("filename", URLEncoder.encode(excelName, "UTF-8"));
        try {
            OutputStream out = response.getOutputStream();
            mediumDateCountExportManageService.doCreateExcel(out, startTime, endTime, excelName);
            out.close();
            System.out.println("excel导出成功！");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }



}
